﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using DLM.Infrastructure.Domain;
using System.ComponentModel;

namespace DLM.Domain.Model
{
    public class Account : IAggregateRoot, IValidatableObject
    {
        public Account()
        {
            this.Users = new HashSet<User>();
        }

        public int AccountID { get; set; }
        [Required(ErrorMessage = "昵称不能为空")]
        //[\w\-]{3,12}
        //[RegularExpression(@"[\w\-]{6,15}", ErrorMessage = "昵称允许6-15位数字，字母和下划线")]
        [RegularExpression(@"^[a-zA-Z][a-zA-Z0-9_]{5,14}$", ErrorMessage = "昵称允许6-15位数字，字母和下划线")]
        [StringLength(15,ErrorMessage="昵称长度最大为15位")]
        public string AccountName { get; set; }

        [Required(ErrorMessage = "密码不能为空")]
        [RegularExpression(@"^(\S+){6,18}$", ErrorMessage = "密码允许6-18位的字符")]
        //[StringLength(15, ErrorMessage = "密码长度最大为15位")]
        public string Password { get; set; }

        [Required(ErrorMessage = "邮箱地址不能为空")]
        [RegularExpression(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$", ErrorMessage = "请输入正确的电子邮箱地址！")]
        public string Email { get; set; }

        public string ActiveCode { get; set; }
        public bool IsActive { get; set; }
        public bool IsValidation { get; set; }
        public System.DateTime CreateDate { get; set; }

        public virtual ICollection<User> Users { get; set; }


        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (string.IsNullOrEmpty(AccountName))
            {
                yield return new ValidationResult("账户名称不能为空", new string[] { "AccountName" });
            }

            if (string.IsNullOrEmpty(Password))
            {
                yield return new ValidationResult("账号密码不能为空", new string[] { "Password" });
            }

            //if (Password.Length < 6)
            //{
            //    yield return new ValidationResult("密码长度不允许小于6位", new string[] { "Password" });
            //}
        }
    }
}
